package org.sjm.samples.web;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.sjm.samples.model.ExamSubjectVO;
import org.sjm.samples.model.Subject;
import org.sjm.samples.service.ISubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class SubjectController {
	
	@Autowired
	private ISubjectService subjectService;
	
	@Autowired
	private LocaleResolver localeResolver;
	
	@ModelAttribute
	public Subject getSubject(){
		return new Subject();
	}
	
	@RequestMapping(value = "/subjectChangeLocale")
	public String changeLocale(HttpServletRequest request, HttpServletResponse response, String localeType){
		if("en_US".equals(localeType)){
			localeResolver.setLocale(request, response, Locale.US);
		}else if("zh_CN".equals(localeType)){
			localeResolver.setLocale(request, response, Locale.CHINA);
		}
		return "redirect:/subjects";
	}

	@RequestMapping("/subjects")
	public String Users(Model model){
		List<ExamSubjectVO> list = subjectService.findAllSubjects();
		for(ExamSubjectVO subjectVO : list){
			if("0".equals(subjectVO.getStatus())){
				subjectVO.setStatus("关闭");
			}else if("1".equals(subjectVO.getStatus())){
				subjectVO.setStatus("开启");
			}else{
				subjectVO.setStatus("");
			}
		}
		model.addAttribute("subjectList", list);
		return "subject/subjectList";
	}
	
	@RequestMapping(value = "/subjectAdd", method = RequestMethod.GET)
	public String toAdd(Model model){
		model.addAttribute("subject", new Subject());
		return "subject/subjectAdd";
	}
	
	@RequestMapping(value = "/subjectAdd", method = RequestMethod.POST)
	public String add(RedirectAttributes redirectAttributes, @Validated ExamSubjectVO subject, BindingResult result){
		if(result.hasErrors()){
			return "subject/subjectAdd";
		}
		try {
			subject.setStatus(subject.getStatus().substring(0,1));
			
			subjectService.createSubject(subject);
			redirectAttributes.addFlashAttribute("message", "添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "redirect:/subjects";
	}
	
	@RequestMapping(value = "/subjectEdit/{subId}", method = RequestMethod.POST)
	public String edit(RedirectAttributes redirectAttributes, ExamSubjectVO subject, @PathVariable String subId){
		try{
			subject.setStatus(subject.getStatus().substring(0,1));
			subjectService.editSubject(subject);
			redirectAttributes.addFlashAttribute("message", "修改成功");
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "redirect:/subjects";
	}
	
	@RequestMapping(value = "/subjectEdit/{subId}", method = RequestMethod.GET)
	public String toEdit(Model model, @PathVariable Integer subId){
		ExamSubjectVO subject = subjectService.getSubjectById(subId);
		model.addAttribute("subject", subject);
		return "subject/subjectEdit";
	}
	
	@RequestMapping(value = "/subjectDelete/{subId}", method = RequestMethod.GET)
	public String delete(RedirectAttributes redirectAttributes, @PathVariable Integer subId) {
		try {
			subjectService.deleteSubject(subId);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", "删除失败");
			e.printStackTrace();
		}
		return "redirect:/subjects";
	}
}
